part of 'card.dart';

class _AudioCardView extends StatelessWidget
    with CardViewMixin, AutoScalableTextMixin, ScreenUtilMixin {
  const _AudioCardView({
    required _AudioCardWloc wloc,
  }) : _wloc = wloc;

  final _AudioCardWloc _wloc;

  double get _scaleFactor => logWidthScaleFactor;

  Widget _buildContent({
    required _AudioCardWlocState state,
    required BuildContext context,
  }) {
    final lexicalItem = _wloc.widget.lexicalItem;

    return Column(
      children: [
        Expanded(
          child: CardContentHorizontalPadding(
            child: LayoutBuilder(
              builder: (context, constraints) {
                final lexicalItemTranscription = getLexicalItemTranscription(
                  lexicalItem: lexicalItem,
                  dialect: state.dialect,
                );

                final autoPronounceable = state.safeToPronounce && state.autoPronunciationEnabled;

                final shouldShowTranscription =
                    lexicalItemTranscription != null && !autoPronounceable;

                final pronunciationButtonSize =
                    kImagelessCardPronunciationButtonSize * _scaleFactor;

                if (!shouldShowTranscription) {
                  return Column(
                    children: [
                      const Spacer(),
                      PronunciationButton(
                        size: pronunciationButtonSize,
                        onPressed: _wloc.onLexicalItemPronunciationButtonPressed,
                      ),
                      const Spacer(),
                    ],
                  );
                }

                final lexicalItemTitleConstraints = BoxConstraints(
                  maxWidth: constraints.maxWidth,
                  maxHeight: (constraints.maxHeight - pronunciationButtonSize) * 0.5,
                );

                final preferredLexicalItemNameTextStyle =
                    HomeCoreTheme.of(context).textTheme.cardLexicalItemName;

                final lexicalItemTitleTextHeight = getTextHeight(
                  text: lexicalItem.title,
                  style: preferredLexicalItemNameTextStyle,
                  textScaleFactor: _scaleFactor,
                );

                final maxLines = getLexicalItemNameMaxLines(
                  textHeight: lexicalItemTitleTextHeight,
                  maxHeight: lexicalItemTitleConstraints.maxHeight,
                );

                final lexicalItemNameMinFontSize =
                    preferredLexicalItemNameTextStyle.fontSize! / kMaxLexicalItemNameShrinkFactor;

                final titleFontSize = calculateFontSize(
                  text: lexicalItem.title,
                  constraints: lexicalItemTitleConstraints,
                  style: preferredLexicalItemNameTextStyle,
                  minFontSize: lexicalItemNameMinFontSize,
                  textScaleFactor: _scaleFactor,
                  maxLines: maxLines,
                );

                return Column(
                  children: [
                    if (shouldShowTranscription) ...[
                      const Spacer(),
                      buildTranscriptionRow(
                        lexicalItemTranscription: lexicalItemTranscription,
                        scaledLexicalItemNameFontSize: titleFontSize,
                        safeToPronounce: state.safeToPronounce,
                        onPronunciationCautionButtonPressed:
                            _wloc.onPronunciationCautionButtonPressed,
                        context: context,
                      ),
                    ],
                    const Spacer(),
                    PronunciationButton(
                      size: pronunciationButtonSize,
                      onPressed: _wloc.onLexicalItemPronunciationButtonPressed,
                    ),
                    const Spacer(),
                  ],
                );
              },
            ),
          ),
        ),
        CardSymbolAssemblageBlock(
          symbolString: lexicalItem.title,
          textEditingController: _wloc.textEditingController,
          textColorAnimationController: _wloc.correctAnswerColorAnimationController,
          textFieldShakeAnimationController: _wloc.wrongAnswerShakeAnimationController,
          onHintButtonPressed: _wloc.onHintButtonPressed,
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return WlocBuilder<_AudioCardWloc, _AudioCardWlocState>(
      wloc: _wloc,
      builder: (context, state) {
        return IgnorePointer(
          ignoring: state.finished,
          child: SafeArea(
            left: false,
            top: false,
            right: false,
            bottom: true,
            maintainBottomViewPadding: true,
            child: LexicalItemCard(
              child: _buildContent(
                state: state,
                context: context,
              ),
            ),
          ),
        );
      },
    );
  }
}
